package com.fh.menu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fh.menu.entity.UmsMenu;
import com.fh.menu.service.IUmsMenuService;
import com.fh.result.ResultObject;
import com.fh.search.MenuParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;


import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台用户权限表 前端控制器
 * </p>
 *
 * @author F
 * @since 2021-05-15
 */
@RestController
@RequestMapping("/menus")
public class UmsMenuController {

    @Autowired
    private IUmsMenuService umsMenuService;

    @GetMapping
    public IPage<UmsMenu> queryMenuList(MenuParam menuParam){
        QueryWrapper<UmsMenu> umsMenuQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(menuParam.getName())){
            umsMenuQueryWrapper.like("name",menuParam.getName());
        }
        umsMenuQueryWrapper.eq("pid",menuParam.getPid());
        umsMenuQueryWrapper.orderByAsc("sort");
        return umsMenuService.page(menuParam,umsMenuQueryWrapper);
    }

    @GetMapping("/parent")
    public ResultObject querymenuList(){
        QueryWrapper<UmsMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",0);
        return ResultObject.success(umsMenuService.list(queryWrapper));
    }

    @PutMapping
    public ResultObject saveOrUpdatemenu(@RequestBody UmsMenu umsMenu){
        return umsMenuService.saveOrUpdateMenu(umsMenu);
    }

    @GetMapping("/{menuId}")
    public ResultObject querymenuById(@PathVariable("menuId") Long menuId){
        return ResultObject.success(umsMenuService.getById(menuId));
    }
   @DeleteMapping("/{menuId}")
    public ResultObject deletemenu(@PathVariable("menuId") Long menuId){
       umsMenuService.removeById(menuId);
        return ResultObject.success();
    }


    @DeleteMapping
    public ResultObject deleteBach(@RequestBody List<Long> idList){
        return umsMenuService.deleteBach(idList);
    }


    @GetMapping("/tree")
    public ResultObject queryMenusZtreeList(){
        return umsMenuService.queryMenusZtreeList();
    }

}

